home *** CD-ROM | disk | FTP | other *** search
- #include "ocHeaders.h"
- #include <LArray.h>
- #include "CEnumConnections.h"
- #include "CConnectionPoint.h"
-
- #define APPENDITEM 32000 // Large number to force appending to an array
-
- //
- // CConnectionPoint::CConnectionPoint
- //
- CConnectionPoint::CConnectionPoint(void* Owner, IID InterfaceID)
- {
- m_Owner = (IUnknown*) Owner;
- m_InterfaceID = InterfaceID;
- m_CookieNext = 100;
-
- m_IUnknown = new LArray(sizeof(IUnknown*));
- m_Cookies = new LArray(sizeof(DWORD));
- }
-
-
- //
- // CConnectionPoint::~CConnectionPoint
- //
- CConnectionPoint::~CConnectionPoint(void)
- {
- delete m_IUnknown;
- delete m_Cookies;
- }
-
-
- //
- // CConnectionPoint::IUnknown::QueryInterface
- //
- // Returns a pointer to the specified interface on a component to which a
- // client currently holds an interface pointer.
- //
- STDMETHODIMP
- CConnectionPoint::QueryInterface(REFIID RefID, void** Obj)
- {
- void* pv;
-
- if (RefID == IID_IUnknown)
- pv = (void*) this;
- else if (RefID == IID_IConnectionPoint )
- pv = (void*)(IConnectionPoint*) this;
- else {
- *Obj = NULL;
- return ResultFromScode(E_NOINTERFACE);
- }
-
- *Obj = pv;
- ((IUnknown*) pv)->AddRef();
- return ResultFromScode(S_OK);
- }
-
-
- //
- // CConnectionPoint::IUnknown::AddRef
- //
- // Increments the reference count for the calling interface.
- //
- STDMETHODIMP_(ULONG)
- CConnectionPoint::AddRef(void)
- {
- return ++m_RefCount;
- }
-
-
- //
- // CConnectionPoint::IUnknown::Release
- //
- // Decrements the reference count for the calling interface on a object. If
- // the reference count on the object falls to zero, the object is freed.
- //
- STDMETHODIMP_(ULONG)
- CConnectionPoint::Release(void)
- {
- if (--m_RefCount != 0)
- return m_RefCount;
-
- delete this;
- return 0;
- }
-
-
-
- //
- // CConnectionPoint::IConnectionPoint::GetConnectionInterface
- //
- STDMETHODIMP
- CConnectionPoint::GetConnectionInterface(IID* InterfaceID)
- {
- *InterfaceID = m_InterfaceID;
-
- return ResultFromScode(S_OK);
- }
-
-
- //
- // CConnectionPoint::IConnectionPoint::GetConnectionPointContainer
- //
- STDMETHODIMP
- CConnectionPoint::GetConnectionPointContainer(IConnectionPointContainer** CPContainer)
- {
- return m_Owner->QueryInterface(IID_IConnectionPointContainer, (void**) CPContainer);
- }
-
-
- //
- // CConnectionPoint::IConnectionPoint::Advise
- //
- STDMETHODIMP
- CConnectionPoint::Advise(IUnknown* UnknownSink, DWORD* Cookie)
- {
- IUnknown* Sink;
-
- // Initialize the cookie
- *Cookie = 0;
-
- // Get the interface this connection point wants from the UnknownSink
- if ( FAILED(UnknownSink->QueryInterface(m_InterfaceID, (void**) &Sink)))
- return ResultFromScode(CONNECT_E_CANNOTCONNECT);
-
- // Append the new sink and cookie
- m_IUnknown->InsertItemsAt(1, APPENDITEM, &Sink);
- m_Cookies->InsertItemsAt(1, APPENDITEM, &m_CookieNext);
-
- // Set the return cookie to the one we assigned
- *Cookie = m_CookieNext++;
-
- return ResultFromScode(S_OK);
- }
-
-
- //
- // CConnectionPoint::IConnectionPoint::Unadvise
- //
- STDMETHODIMP
- CConnectionPoint::Unadvise(DWORD Cookie)
- {
- ArrayIndexT i;
- ErrorCode Result = S_OK;
-
- if ((i = m_Cookies->FetchIndexOf(&Cookie)) == LArray::index_Bad)
- Result = E_FAIL;
- else
- {
- IUnknown* Sink;
- m_IUnknown->FetchItemAt(i, &Sink);
- m_IUnknown->RemoveItemsAt(1, i);
- m_Cookies->RemoveItemsAt(1, i);
- Sink->Release();
- }
-
- return Result;
- }
-
-
- //
- // CConnectionPoint::IConnectionPoint::EnumConnections
- //
- STDMETHODIMP
- CConnectionPoint::EnumConnections(IEnumConnections** EnumConnections)
- {
- LArray* ConnectArray;
- CONNECTDATA ConnectData;
- unsigned long i, NumConnected;
- CEnumConnections* EnumObject;
-
- // No items connected, return failure
- if ( (NumConnected = m_IUnknown->GetCount()) == 0 )
- return ResultFromScode(E_FAIL);
-
- // Allocate an array for the CONNECTDATA
- ConnectArray = new LArray(sizeof(CONNECTDATA));
-
- // Move all the pertinent information from our list of IUnknowns and cookies
- for ( i = 1; i <= NumConnected; i++)
- {
- m_IUnknown->FetchItemAt(i, &ConnectData.pUnk);
- m_Cookies->FetchItemAt(i, &ConnectData.dwCookie);
- ConnectArray->InsertItemsAt(1, i, &ConnectData);
- }
-
- // Create a new enumerator object
- EnumObject = new CEnumConnections(ConnectArray);
-
- // Get the IEnumConnections interface pointer
- EnumObject->QueryInterface(IID_IEnumConnections, (void**) EnumConnections);
-
- return ResultFromScode(S_OK);
- }
-
-
-